package com.wfcm.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wfcm.entity.WfCourseChapterEntity;
import com.wfcm.service.WfCourseChapterService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfcm.annotation.IgnoreToken;
import com.wfcm.entity.WfCourseInfoEntity;
import com.wfcm.entity.WfKeywordEntity;
import com.wfcm.entity.WfVipCardEntity;
import com.wfcm.service.WfCourseInfoService;
import com.wfcm.service.WfKeywordService;
import com.wfcm.service.WfVipCardService;
import com.wfcm.utils.R;

/**
 * 
 * @content 搜索模块Controller
 */
@RequestMapping("/search")
@Controller
public class IndexSearchController {
	
	private Logger log = LoggerFactory.getLogger(IndexRecommendController.class);
	@Autowired
	private WfCourseInfoService courseInfoService;
	@Autowired
	private WfVipCardService vipCardService;
	@Autowired
	private WfKeywordService keywordService;
	@Autowired
	private WfCourseChapterService courseChapterService;
	
	/**
	 * 搜索课程
	 *
	 */
	@RequestMapping("/course")
	@ResponseBody
	@IgnoreToken
	public R searchCourse(String keywords) {
		Map<String, Object> map = new HashMap<>();
		map.put("keywords", keywords);
		List<WfCourseInfoEntity> courseInfolist = courseInfoService.querySearchList(map);
		List<WfVipCardEntity> vipCardlist = vipCardService.querySearchList(map);
		List<WfCourseChapterEntity> courseChapterList = courseChapterService.querySearchList(map);
		WfKeywordEntity wfKeyword = new WfKeywordEntity();
		wfKeyword.setKeyword(keywords);
		wfKeyword.setIstop(2);
		wfKeyword.setSearchtime(new Date());
		keywordService.save(wfKeyword);
		map.put("courseInfolist", courseInfolist);
		map.put("vipCardlist", vipCardlist);
		map.put("courseChapterlist", courseChapterList);
		return R.ok(map);
		
	}
	
	/**
	 * 搜索课程
	 *  
	 * H5调用接口，中文传参会乱码，所有专门为其提供接口
	 */
	@RequestMapping("/H5course")
	@ResponseBody
	@IgnoreToken
	public R searchH5Course(HttpServletRequest req, String keywords) {
		System.out.println("test -- " + keywords);
//		String keywords = null;
		try {

//			keywords = new String(req.getParameter("keywords").getBytes("ISO8859-1"), "UTF-8");
			System.out.println("**************************************转码获取:"+keywords);
			if(StringUtils.isBlank(keywords)) {
				keywords =  req.getParameter("keywords");
				System.out.println("**************************************直接获取:"+keywords);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String k =  req.getParameter("keywords");
		System.out.println("**************************************直接获取k:"+k );
		System.out.println("**************************************最终结果："+keywords);
		//String keywords =  req.getParameter("keywords");
		Map<String,Object>  map = new HashMap<String,Object>();
		map.put("keywords", keywords);
		
		//查询相关课程
		List<WfCourseInfoEntity> courseInfolist =  new ArrayList<WfCourseInfoEntity>();
		courseInfolist = courseInfoService.querySearchList(map);
		//查询相关课程包
		List<WfVipCardEntity> vipCardlist =  new ArrayList<WfVipCardEntity>();
		vipCardlist = vipCardService.querySearchList(map);
		//查询相关课程包
		WfKeywordEntity wfKeyword = new WfKeywordEntity();
		wfKeyword.setKeyword(keywords);
		wfKeyword.setIstop(2);
		wfKeyword.setSearchtime(new Date());
		keywordService.save(wfKeyword);
		
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		resultMap.put("courseInfolist", courseInfolist);
		resultMap.put("vipCardlist", vipCardlist);
		System.out.println("*********************************"+resultMap.toString());
		return R.ok(resultMap);
		
	}
	
	
	/**
	 * 热词搜索
	 */
	@RequestMapping("/searchHotKeyword")
	@ResponseBody
	@IgnoreToken
	public R searcHotKeyword(HttpServletRequest req) {
		
		Map<String,Object>  map = new HashMap<String,Object>();
		
		List<WfKeywordEntity> keywordList =  keywordService.queryList(map);
		
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		resultMap.put("keywordList", keywordList);
		
		return R.ok(resultMap);
	}
}
